package
{
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2World;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * The top level control structure.
	 */
	public class Game extends Sprite
	{
		/** The total list of game objects. */
		private var game_objects:Vector.<GameObject>;
		/** The physics world from Box2D */
		private var world:b2World;
		/** The surface that is drawn directly on the screen. */
		private var bitmap:Bitmap;
		
		/**
		 * Create a new Game.
		 */
		public function Game()
		{
			
		}
		
		/**
		 * Initialize the game. Call after added to the stage.
		 */
		public function init():void
		{
			bitmap = new Bitmap();
			bitmap.bitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
			
			game_objects = new Vector.<GameObject>();
			
			// create a world with no gravity and allow still bodies to sleep.
			world = new b2World(new b2Vec2(0, 0), true);
		}
		
		/**
		 * Called every frame.
		 * @param	e	the enter frame event
		 */
		public function onEnterFrame(e:Event):void
		{
			// updates the physics world. All bodies are moved.
			world.Step(1 / stage.frameRate, 5, 5); // TODO: Make these constants
			// Remove all forces applied to bodies this frame
			world.ClearForces();
			
			// update all GameObjects
			for each (var to_update:GameObject in game_objects)
			{
				to_update.update();
			}
			
			// draw all GameObjects on the main surface.
			for each (var to_draw:GameObject in game_objects)
			{
				to_draw.render(bitmap.bitmapData);
			}
		}
	}

}